<?php
class Controller_API_User extends Controller
{
    public function __construct ($req, $res)
    {
        parent::__construct($req, $res);
        $this->_post = $this->request->post();
        $this->_query= $this->request->query();
    }

    public function action_login ()
    {
        Kohana::$log->add(Log::DEBUG, "User=>Login, POST:\n" . print_r($this->_post, TRUE));

        $post = $this->_post;

        $source = Arr::get($post, 'source', '');
        $userId = Arr::get($post, 'userId', '');
        $userToken = Arr::get($post, 'userToken', '');
        $userName = Arr::get($post, 'userName', '');
        $userIcon = Arr::get($post, 'userIcon', '');
        $userGender = Arr::get($post, 'userGender', '');
        $userPushId = Arr::get($post, 'userPushId', '');

        $user = user::initUserFromBasicInfo ($source, $userId, $userToken, $userName, $userIcon, $userGender, $userPushId);
        $uid  = $user->getUserUid();
        $icon = $user->getUserIcon();
        $push = $user->getUserPushId();

        echo json_encode(array(
            'status' => 0,
            'userUid' => $uid,
            'userIcon' => $icon,
            'pushId'   => $push,
        ));
    }

    public function action_auth ()
    {
        Kohana::$log->add(Log::DEBUG, "User=>Auth, GET:\n" . print_r($this->_query, TRUE));

        $post = $this->_query;

        $source = Arr::get($post, 'source', '');
        $userId = Arr::get($post, 'userId', '');
        $userToken = Arr::get($post, 'userToken', '');
        $userName = Arr::get($post, 'userName', '');
        $userIcon = Arr::get($post, 'userIcon', '');
        $userGender = Arr::get($post, 'userGender', '');
        $userPushId = Arr::get($post, 'userPushId', '');

        $user = user::initUserFromBasicInfo ($source, $userId, $userToken, $userName, $userIcon, $userGender, $userPushId);
        $uid  = $user->getUserUid();
        if ($uid > 0)
        {
            Passport::setCurrentUserUid($uid);
            $this->request->redirect('/');
        }
        else
        {
            $this->request->redirect('/login');
        }
    }

    public function action_like ()
    {
        $type = Arr::get($this->_post, 'type'); 
        $nid  = Arr::get($this->_post, 'nid');

        $user = user::currentUser();
        if ($user->getUserUid() == 0)
            return;

        $result = App_Item::factory($type, $nid)->like($user);
        $hasLiked = $user->hasAction($type, $nid, NULL);

        if (FALSE === $result)
            $ret = array('status' => 1, 'errormsg' => '发生错误，请重试');
        else
            $ret = array('status' => 0, 'counts' => $result, 'hasLiked' => (int) $hasLiked);

        Kohana::$log->add(Log::DEBUG, "user_like: $type, $nid, $result");
        echo json_encode($ret);
    }

    public function action_follow ()
    {
        $user = user::currentUser();
        $uid = $user->getUserUid();
        if (!($uid > 0))
        {
            echo json_encode(array('status' => 1, 'errmsg' => '未登录'));
            exit;
        }
        
        $type = Arr::get($this->_post, 'type');
        $uid  = Arr::get($this->_post, 'uid');

        try {

            $follow = new App_Follow($user->getUserUid());
            $follow->followUser($type, $uid);
            $ret = array('status' => 0);

        }catch (Exception $e)
        {
            $ret = array('status' => 1);
        }
        
        echo json_encode($ret); 
    }

    public function action_like_shop ()
    {
        $type = Arr::get($this->_post, 'type');
        $nid  = Arr::get($this->_post, 'nid');

        $user = user::currentUser();
        $result = App_Item::factory($type, $nid)->like($user);
        $hasLiked = $user->hasAction($type, $nid, NULL);

        if (FALSE === $result)
            $ret = array('status' => 1, 'errormsg' => '发生错误，请重试');
        else
            $ret = array('status' => 0, 'counts' => $result, 'hasLiked' => (int) $hasLiked);

        Kohana::$log->add(Log::DEBUG, "user_like: $type, $nid, $result");
        echo json_encode($ret);
    }

    public function action_share ()
    {
        $type = Arr::get($this->_post, 'type'); 
        $nid  = Arr::get($this->_post, 'nid');

        $user = user::currentUser();
        $uid  = $user->getUserUid();
        $result = App_Item::factory($type, $nid)->share($user);
        $user->addActionShare($type, $nid);

        $ret = array('status' => 0);

        Kohana::$log->add(Log::DEBUG, "user_share: $type, $nid, $uid");
        echo json_encode($ret);
    }

    public function action_comment ()
    {
        $type = Arr::get($this->_post, 'type'); 
        $nid  = Arr::get($this->_post, 'nid');
        $message = Arr::get($this->_post, 'message');
        $touid   = Arr::get($this->_post, 'touid', 0);
        $toid    = Arr::get($this->_post, 'toid', 0);
        Kohana::$log->add(Log::DEBUG, "comment: $type, $nid, $message, $touid, $toid");

        $comment = new App_Comments($type, $nid);
        $user = User::currentUser();
        $id = $comment->add($user, $message, $toid, $touid);
        if ($id > 0)
        {
            list($total, $items) = App_Comments::items($type, $nid, 0, 10, $id);
            $ret = array('status' => 0, 'items' => $items, 'total' => $total);
        }
        else
        {
            $ret = array('status' => 1,);
        }

        echo json_encode($ret);
    }

    public function action_bindPushDeviceId () 
    {
        $deviceId = Arr::get($this->_post, 'deviceId', '');        
        $user = User::currentUser();
        $uid  = $user->getUserUid();

        Kohana::$log->add(Log::DEBUG, "bindPushDeviceId: $uid, $deviceId");
        $ret = array('status' => 0);
        echo json_encode($ret);
    }
}
